package com.meida.module.pay.provider.service.impl;

import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.model.DeleteModel;
import com.meida.common.mybatis.model.PageModel;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.model.SaveModel;
import com.meida.common.mybatis.query.CriteriaDelete;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.ApiAssert;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.pay.client.entity.Invoice;
import com.meida.module.pay.client.entity.InvoiceReceiver;
import com.meida.module.pay.client.entity.PayInfo;
import com.meida.module.pay.provider.mapper.PayInvoiceMapper;
import com.meida.module.pay.provider.service.PayInvoiceReceiverService;
import com.meida.module.pay.provider.service.PayInvoiceService;
import com.meida.module.pay.provider.service.PayInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发票 实现类
 *
 * @author flyme
 * @date 2019-06-26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PayInvoiceServiceImpl extends BaseServiceImpl<PayInvoiceMapper, Invoice> implements PayInvoiceService {

    @Autowired
    private PayInvoiceReceiverService invoiceReceiverService;

    @Autowired
    private PayInfoService payInfoService;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody pageList(PageModel model) {
        CriteriaQuery cq = new CriteriaQuery(model, Invoice.class);
        Long userId = OpenHelper.getUserId();
        cq.select(Invoice.class, "invoiceId", "invoiceTitle", "invoiceAmount", "invoiceType", "address");
        cq.select(InvoiceReceiver.class, "bankName", "bankNo", "creditCode", "telephone");
        cq.likeByField(Invoice.class, "invoiceTitle");
        cq.eq(Invoice.class, "userId", userId);
        cq.createJoin(InvoiceReceiver.class);
        return basePageList(cq);
    }


    @Override
    public ResultBody add(SaveModel model) {
        CriteriaSave cs = new CriteriaSave(model, Invoice.class);
        Long userId = OpenHelper.getUserId();
        Long orderId = cs.getLongParams("orderId");
        ApiAssert.isNotEmpty("orderId不能为空", orderId);
        PayInfo payInfo = payInfoService.getById(orderId);
        Long invoiceId = payInfo.getInvoiceId();
        ApiAssert.isEmpty("发票已申请,不可重复申请", invoiceId);
        Long invoiceReceiverId = cs.getLongParams("invoiceReceiverId");
        if (FlymeUtils.isNotEmpty(invoiceReceiverId)) {
            InvoiceReceiver invoiceReceiver = invoiceReceiverService.getById(invoiceReceiverId);
            cs.put("consignee", invoiceReceiver.getUserName());
            cs.put("address", invoiceReceiver.getAddress());
            cs.put("invoiceReceiverId", invoiceReceiverId);
        }
        cs.put("userId", userId);
        ResultBody<Invoice> resultBody = baseAdd(cs);
        Invoice invoice = resultBody.getData();
        //更新订单开票ID
        payInfoService.updateInvoiceId(orderId, invoice.getInvoiceId());
        return resultBody;
    }

    @Override
    public ResultBody delete(DeleteModel model) {
        CriteriaDelete cd = new CriteriaDelete(model, Invoice.class);
        Long[] ids = model.getIds();
        //清空订单关联发票ID
        payInfoService.clearInvoiceId(ids);
        return baseDelete(cd);
    }

}
